package com.jiang.专题.哈希表.Q697;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/04/17 17:21
 */
public class Solution {
    public int findShortestSubArray(int[] nums) {
        Map<Integer, int[]> map = new HashMap<>();
        int cnt = 0;
        int len = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            if (!map.containsKey(nums[i])) {
                map.put(nums[i], new int[]{1, i});
            } else {
                map.get(nums[i])[0] += 1;
            }
            int a = map.get(nums[i])[0], b = i - map.get(nums[i])[1] + 1; //a为出现次数，b为子数组长度
            if (a > cnt || (a == cnt && b < len)) {
                cnt = a;
                len = b;
            }
        }
        return len;
    }
}
